/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-5-24下午05:47:39
 */
package hit.edance.campusmobile.chat.database;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**   
 * 登陆过程中的数据库操作
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.database
 * @Class DBLoginHelper
 * @Date 2012-5-24 下午05:47:39
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class DBLoginHelper {
	private static final String DEBUG_TAG = "DbLoginHelper"; 
	
	private static final String TABLE_NAME = "AutoAccount";  //表名
	
	private final Context mCtx;
	
	private DBUtil db;
	
	private SQLiteDatabase mDb;	//声明数据库对象
	
	
	
	/**
	 * 构造方法中初始化Context对象
	 * @param ctx
	 */
	public DBLoginHelper(Context ctx) {
		this.mCtx = ctx;
		db = new DBUtil(mCtx);
	}
	/**
	 * 向数据库中插入account
	 * @param account
	 * @return 成功或失败
	 */
	public boolean insertAccount(String account) {
		final String sql = "insert into "+TABLE_NAME+" (account) values('"+account+"');";
		try{
			mDb = db.open().getWritableDB();
			mDb.execSQL(sql);
			Log.i(DEBUG_TAG+":"+TABLE_NAME,sql);
			return true;
		}catch(SQLException e){
			return false;
		}finally{
			db.close();
		}
		
	}

	/**
	 * @return 返回所有的account的Cursor
	 */
	public List<String> getAccounts() {
		Cursor cursor = null;
		try {
			mDb = db.open().getReadableDB();
			cursor = mDb.query(TABLE_NAME, new String[] { "account" }, null,
					null, null, null, null);
			List<String> uidList = new ArrayList<String>();
			int columnIndex = cursor.getColumnIndex("account");
			while (cursor.moveToNext()) {
				uidList.add(cursor.getString(columnIndex));
			}
			return uidList;

		} finally {
			if(cursor != null)
				cursor.close();
			db.close();
		}
	}
}
